﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor
{
    public partial class SatisEtiket : DevReport
    { 
        public SatisEtiket(int CikisEmriId)
        {
            InitializeComponent();

            myData1.xEtiket.Merge(ExecuteDataTable("GetSatisBarcode", CikisEmriId)); 

            MakeResizable(xrTableCell11, xrTableCell24);
        }



        #region MakeResizable
        Dictionary<string, Font> defaultFonts = new Dictionary<string, Font>();
        void MakeResizable(params XRControl[] controls)
        {
            foreach (XRTableCell cell in controls)
            {
                defaultFonts.Add(cell.Name, cell.Font);
                cell.BeforePrint += new PrintEventHandler(cell_BeforePrint);
            }
        }

        void cell_BeforePrint(object sender, PrintEventArgs e)
        {
            Resize((XRControl)sender);
        }

        private void Resize(XRControl cell)
        {
            //Resize ı table'larda düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            Font defaultFont = defaultFonts[cell.Name];
            float newSize = defaultFont.Size;

            float stringWidth = gr.MeasureString(cell.Text, defaultFont).Width;
            float cellWidth = cell.WidthF;

            if (cellWidth < stringWidth * factor)
                newSize = defaultFont.Size * cell.Width / stringWidth / factor;

            Font newFont = new Font(defaultFont.Name, newSize, defaultFont.Style, defaultFont.Unit, defaultFont.GdiCharSet, defaultFont.GdiVerticalFont);

            cell.Font = newFont;

            gr.Dispose();
        }
        #endregion
          
    }
}